package com.gewu.pm.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.AttendanceRecordChildBean;
import com.hjq.shape.view.ShapeImageView;

/**
 * 下属打卡记录子列表适配器
 */
public final class SubordinateAttendanceChildAdapter extends AppAdapter<AttendanceRecordChildBean> {

    private OnItemActionListener mListener;

    public SubordinateAttendanceChildAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    public void setOnItemActionListener(OnItemActionListener listener) {
        mListener = listener;
    }

    private final class ViewHolder extends AppAdapter<AttendanceRecordChildBean>.ViewHolder {

        private final ShapeImageView ivAvatar;
        private final TextView tvUserName;
        private final LinearLayout llSignIn;
        private final View vSignInLine;
        private final TextView tvSignInTime;
        private final TextView tvSignInStatus;
        private final TextView tvViewPhoto;

        private final LinearLayout llSignOut;
        private final TextView tvSignOutTime;
        private final TextView tvSignOutStatus;
        private final TextView tvViewPhotoOut;

        private ViewHolder() {
            super(R.layout.subordinate_clock_in_child_item);

            ivAvatar = findViewById(R.id.iv_avatar);
            tvUserName = findViewById(R.id.tv_user_name);
            llSignIn = findViewById(R.id.ll_sign_in);
            vSignInLine = findViewById(R.id.v_sign_in_line);
            tvSignInTime = findViewById(R.id.tv_sign_in_time);
            tvSignInStatus = findViewById(R.id.tv_sign_in_status);
            tvViewPhoto = findViewById(R.id.tv_view_photo);

            llSignOut = findViewById(R.id.ll_sign_out);
            tvSignOutTime = findViewById(R.id.tv_sign_out_time);
            tvSignOutStatus = findViewById(R.id.tv_sign_out_status);
            tvViewPhotoOut = findViewById(R.id.tv_view_photo_out);
        }

        @Override
        public void onBindView(int position) {
            AttendanceRecordChildBean bean = getItem(position);

            // 用户信息
            tvUserName.setText(bean.getUserName());
            // TODO: 加载头像 Glide.with(getContext()).load(bean.getUserAvatar()).into(ivAvatar);

            // 签到信息
            int checkStatus = bean.getCheckStatus();
            String signInTime = bean.getSignInTime();

            if (TextUtils.isEmpty(signInTime)) {
                // 未打卡
                tvSignInTime.setText("未打卡");
                tvSignInStatus.setVisibility(View.GONE);
                tvViewPhoto.setVisibility(View.VISIBLE);
                tvViewPhoto.setText(R.string.view_fill_description);
            } else {
                tvSignInTime.setText(signInTime);
                if (checkStatus == 2) {
                    // 迟到
                    tvSignInStatus.setVisibility(View.VISIBLE);
                    tvSignInStatus.setText(R.string.late);
                    tvViewPhoto.setVisibility(View.VISIBLE);
                    tvViewPhoto.setText(R.string.view_photo);
                } else {
                    // 正常
                    tvSignInStatus.setVisibility(View.GONE);
                    tvViewPhoto.setVisibility(View.VISIBLE);
                    tvViewPhoto.setText(R.string.view_photo);
                }
            }

            // 签退信息
            int signOffStatus = bean.getSignOffStatus();
            String signOutTime = bean.getSignOutTime();

            if (TextUtils.isEmpty(signOutTime)) {
                // 未打卡
                tvSignOutTime.setText("--:--:--");
                tvSignOutStatus.setVisibility(View.GONE);
                tvViewPhotoOut.setVisibility(View.GONE);
            } else {
                tvSignOutTime.setText(signOutTime);
                if (signOffStatus == 2) {
                    // 早退
                    tvSignOutStatus.setVisibility(View.VISIBLE);
                    tvSignOutStatus.setText(R.string.early_leave);
                    tvViewPhotoOut.setVisibility(View.VISIBLE);
                    tvViewPhotoOut.setText(R.string.view_photo);
                } else {
                    // 正常
                    tvSignOutStatus.setVisibility(View.GONE);
                    tvViewPhotoOut.setVisibility(View.VISIBLE);
                    tvViewPhotoOut.setText(R.string.view_photo);
                }
            }

            // 时间轴竖线（最后一条记录不显示）
            vSignInLine.setVisibility(position == getItemCount() - 1 ? View.GONE : View.VISIBLE);

            // ✅ BaseAdapter 会自动处理 itemView 的点击事件
            // 子View的点击事件通过自定义接口处理
            tvViewPhoto.setOnClickListener(v -> {
                if (mListener != null) {
                    if (checkStatus == 0) {
                        mListener.onViewDescription(position, bean, true);
                    } else {
                        mListener.onViewPhoto(position, bean, true);
                    }
                }
            });

            tvViewPhotoOut.setOnClickListener(v -> {
                if (mListener != null) {
                    mListener.onViewPhoto(position, bean, false);
                }
            });
        }
    }

    /**
     * 子View点击事件接口（不包括Item整体点击）
     */
    public interface OnItemActionListener {
        /**
         * 查看说明
         * @param isSignIn true=签到，false=签退
         */
        void onViewDescription(int position, AttendanceRecordChildBean bean, boolean isSignIn);

        /**
         * 查看图片
         * @param isSignIn true=签到，false=签退
         */
        void onViewPhoto(int position, AttendanceRecordChildBean bean, boolean isSignIn);
    }
}
